package bb.lanxing.calc.gps;

import android.os.Handler;
import android.os.Looper;
import com.umeng.analytics.MobclickAgent;

import java.io.IOException;

import bb.lanxing.App;
import bb.lanxing.common.config.UmengEventConst;
import bb.lanxing.manager.SystemLocationManager;
import bb.lanxing.util.Log;

public class GPSSearchHeartbeatStrategy implements GPSSearchStrategy {
    private static final int CHECK_INTERVAL = 10000;
    private static final int LOCATION_TIMEOUT = 120000;
    private static final int MAX_CHECK_INTERVAL = 60000;
    private static final int MIN_CHECK_INTERVAL = 20000;
    private static final String TAG = "GPSSearchHeartbeat";
    private boolean gpsOpen;
    private Handler handler;
    private boolean hasGpsLocated;
    private long lastGpsStartTime;
    private final int locateTimeout;
    private int rebootGpsTime;

    public GPSSearchHeartbeatStrategy() {
        this(LOCATION_TIMEOUT);
    }

    public GPSSearchHeartbeatStrategy(int locateTimeout) {
        this.gpsOpen = false;
        this.rebootGpsTime = 20000;
        this.locateTimeout = locateTimeout;
        this.handler = new Handler(Looper.getMainLooper());
    }

    @Override
    public void onGpsStatus(boolean succeed) {
        if (this.gpsOpen) {
            long currentTimeMillis = System.currentTimeMillis();
            if (succeed) {
                if (!this.hasGpsLocated) {
                    this.hasGpsLocated = true;
                    MobclickAgent.onEventValue(App.getContext(), UmengEventConst.GPS_LOCATED_TIME, null, (int) (currentTimeMillis - this.lastGpsStartTime));
                    MobclickAgent.onEventValue(App.getContext(), UmengEventConst.V30_GPS_LOCATED_TIME, null, (int) (currentTimeMillis - this.lastGpsStartTime));
                }
                this.rebootGpsTime = 20000;
                this.lastGpsStartTime = currentTimeMillis;
                if (SystemLocationManager.getInstance().isSystemGpsWorked()) {
                    return;
                }
                try {
                    SystemLocationManager.getInstance().openSystemGps();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } else if (this.hasGpsLocated && SystemLocationManager.getInstance().isSystemGpsWorked() && currentTimeMillis - this.lastGpsStartTime > this.locateTimeout) {
                this.handler.postDelayed(GPSSearchHeartbeatStrategy.this::resumeGps, this.rebootGpsTime);
                pauseGps();
            }
        }
    }

    @Override
    public void onGpsSwitch(boolean open) {
        this.gpsOpen = open;
        if (open) {
            this.rebootGpsTime = 20000;
            this.lastGpsStartTime = System.currentTimeMillis();
            this.hasGpsLocated = false;
            return;
        }
        this.handler.removeCallbacksAndMessages(null);
    }

    private void pauseGps() {
        Log.i(TAG, "pauseGps: ");
        SystemLocationManager.getInstance().closeSystemGps();
        int i = this.rebootGpsTime + 10000;
        this.rebootGpsTime = i;
        if (i > 60000) {
            this.rebootGpsTime = 60000;
        }
    }

    public void resumeGps() {
        Log.i(TAG, "resumeGps: ");
        this.lastGpsStartTime = System.currentTimeMillis();
        try {
            SystemLocationManager.getInstance().openSystemGps();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
